<template>
  <a-modal
    :title="formTile + '供应商价格方案'"
    :visible="true"
    width="50%"
    :footer="null"
    @cancel="reset"
  >
    <div class="main">
      <a-card>
        <a-form-model
          ref="ruleForm"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          :model="form"
          :rules="rules"
        >
          <a-form-model-item label="供应商" prop="supplyCode">
            <a-select allow-clear v-model="form.supplyCode" placeholder="请选择供应商" show-search @search="handleSearch" @change="handleChange"
            :filterOption="filterOption">
              <a-select-option v-for="(item) in supplyList" :key="item.code" :value="item.code" :title="item.name">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item
            label="方案编号"
            prop="code"
          >
            <a-input
              placeholder="请输入方案编号"
              v-model="form.code"
              allow-clear
            />
          </a-form-model-item>
          <a-form-model-item
            label="方案名称"
            prop="name"
          >
            <a-input
              placeholder="请输入方案名称"
              v-model="form.name"
              allow-clear
            />
          </a-form-model-item>
          <a-form-model-item
            label="计算方案"
            prop="calculationCode"
          >
            <a-select
            allow-clear
              v-model="form.calculationCode"
              placeholder="请选择计算方案"
              show-search
              @change="handleChange"
              :filterOption="filterOption"
            >
              <a-select-option
                v-for="(item) in nameList"
                :key="item.code"
                :value="item.code"
                :title="item.name"
              >
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item
            label="0CM基价"
            prop="basePrice"
          >
            <a-input
            allow-clear
              placeholder="请输入0CM基价"
              v-model="form.basePrice"
            />
          </a-form-model-item>
          <a-form-model-item
            label="每CM单价"
            prop="unitPrice"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入每CM单价"
              v-model="form.unitPrice"
            />
          </a-form-model-item>
          <a-form-model-item
            label="最小单位"
            prop="minQty"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入最小单位"
              v-model="form.minQty"
            />
          </a-form-model-item>
          <a-form-model-item
            label="基准单价"
            prop="minPrice"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入基准单价"
              v-model="form.minPrice"
            />
          </a-form-model-item>
           <!-- <a-form-model-item
            label="最小长度"
            prop="minValue"
          >
          <a-input
            allow-clear
            type="number"
              placeholder="请输入最小长度"
              v-model="form.minValue"
            />
          </a-form-model-item> -->
          <a-form-model-item
            label="小于基长每CM单价"
            prop="lunitPrice"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入小于基长每CM单价"
              v-model="form.lunitPrice"
            />
          </a-form-model-item>
          <a-form-model-item
            label="小于基长特殊价格"
            prop="lotherPrice"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入小于基长特殊价格"
              v-model="form.lotherPrice"
            />
          </a-form-model-item>
          <a-form-model-item
            label="小于基长基价"
            prop="lbasePrice"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入小于基长基价"
              v-model="form.lbasePrice"
            />
          </a-form-model-item>
          <a-form-model-item
            label="特殊价格"
            prop="other"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入特殊价格"
              v-model="form.other"
            />
          </a-form-model-item>
          <a-form-model-item
            label="折扣"
            prop="discount"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入折扣"
              v-model="form.discount"
            />
          </a-form-model-item>
          <a-form-model-item
            label="税率"
            prop="cess"
          >
            <a-input
            allow-clear
            type="number"
              placeholder="请输入税率"
              v-model="form.cess"
            />
          </a-form-model-item>
          <a-form-model-item
            label="备注"
            prop="remark"
          >
            <a-textarea
            allow-clear
              placeholder="请输入备注"
              v-model="form.remark"
              :rows="4"
            />
        
          </a-form-model-item>
          <a-form-model-item>
            <a-row style="margin-top:10px">
              <a-col
                :span="16"
                :offset="18"
                class="info-action"
              >
                <a-button @click="reset">
                  重置
                </a-button>
                <a-button
                  :style="{ marginLeft: '8px' }"
                  type="primary"
                  @click="onSubmit"
                  :loading="loading"
                >
                  保存
                </a-button>
              </a-col>
            </a-row>
          </a-form-model-item>
        </a-form-model>
      </a-card>
    </div>

  </a-modal>
</template>

<script>
import { postAction } from '@/api/manage'
import { seachSupply,getDictChildList } from '@api/material/index'


export default {
  components: {},

  props: {
    supplyPriceSchemeId: {
      type: Number,
      default: null
    }
  },

  data() {
    return {
      urlKey: 'basemgt-material/accessories/',
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      nameList: [],
      supplyList: [],
      form: {
        supplyCode:undefined,
        name: '',
        code: '',
        calculationCode:undefined,
        basePrice:'',
        unitPrice:'',
        minQty:'',
        minPrice:'',
        minValue:0,
        lunitPrice:'',
        lotherPrice:'',
        lbasePrice:'',
        other:'',
        discount:'',
        cess:'',
        remark:'',
      },
      rules: {
        supplyCode: [{ required: true, message: '请选择供应商', trigger: 'blur' }],
        code: [{ required: true, message: '请输入方案编号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入方案名称', trigger: 'blur' }],
        calculationCode: [{ required: true, message: '请选择计算方案', trigger: 'blur' }],
        basePrice: [{ required: true, message: '请输入0CM基价', trigger: 'change' }],
        unitPrice: [{ required: true, message: '请输入每CM单价', trigger: 'change' }],
        minQty: [{ required: true, message: '请输入基准单位长度', trigger: 'change' }],
        minPrice: [{ required: true, message: '请输入最小单价', trigger: 'change' }],
        minValue: [{ required: true, message: '请输入最小长度', trigger: 'change' }],
        lunitPrice: [{ required: true, message: '请输入小于基长每CM单价', trigger: 'change' }],
        lotherPrice: [{ required: true, message: '请输入小于基长特殊价格', trigger: 'change' }],
        lbasePrice: [{ required: true, message: '请输入小于基长基价', trigger: 'change' }],
        other: [{ required: true, message: '请输入特殊价格', trigger: 'change' }],
        discount: [{ required: true, message: '请输入折扣', trigger: 'change' }],
        cess: [{ required: true, message: '请输入税率', trigger: 'change' }],
        remark: [{ required: true, message: '请输入备注', trigger: 'change' }],
      },
      loading: false
    }
  },

  created() {
    const { supplyPriceSchemeId } = this.$props
    if (supplyPriceSchemeId) {
      this.formTile = '编辑'
      this.getDetail()
    } else {
      this.formTile = '新增'
    }
    this.getDictChildList()
    this.handleSearch()
  },
  mounted() {},
  computed: {
    jobNumber() {
      return this.$store.getters.userInfo.workNo || ''
    }
  },
  methods: {
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.submit()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleChange(value) {
      console.log(value)
    },
    filterOption (value, option) {
        return option.componentOptions.children[0].text.indexOf(value) >= 0
    },
     async handleSearch(value) {

      const res = await seachSupply({ keyWord: value })
      if (res.code == 200) {
        this.supplyList = res.data
      } else {
        if (res.msg) {
          this.$message.error(res.msg)
        } else {
          this.$message.error('获取供应商列表失败！')
        }
      }
    },
    async getDictChildList() {
      const params = {
        name: 'ACC_SUPPLY_PRICE_SCHEME'
      }
      try {
        const res = await getDictChildList(params)
        if (res.code == 200) {
          this.nameList = res.data
        } else {
          if (res.msg) {
            this.$message.error(res.msg)
          } else {
            this.$message.error('获取方案列表失败！')
          }
        }
      } catch (error) {
        this.$message.error('获取方案列表失败！')
      }
    },
    reset() {
      this.$refs.ruleForm.resetFields()
      this.$emit('close')
    },
    getDetail() {
      const params = {
        supplyPriceSchemeId: this.supplyPriceSchemeId
      }
      try {
        postAction(`${this.urlKey}supply/price/scheme/info`, params).then(res => {
          if (res.code == 200) {
            this.form = res.data
          } else {
            this.$message.warning(res.msg)
          }
        })
      } catch (error) {
        this.$message.warning('获取详情失败！')
      }
    },
    submit() {
      this.loading = true
      const params = JSON.parse(JSON.stringify(this.form))
      params.jobNumber = this.jobNumber
      if(this.supplyPriceSchemeId){
        params.supplyPriceSchemeId = this.supplyPriceSchemeId
      }
      const FNCName = this.supplyPriceSchemeId ? `${this.urlKey}supply/price/scheme/update` : `${this.urlKey}supply/price/scheme/save`
      try {
        postAction(FNCName, params).then(res => {
          if (res.code == 200) {
            this.$message.success(this.supplyPriceSchemeId ? '编辑成功！' : '新增成功！')
            this.reset()
          } else {
            this.$message.warning(res.msg)
          }
        })
      } catch (error) {
        this.$message.warning('操作失败！')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
<style scoped>
.info-action {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
}
.ml-20 {
  margin-left: 20px;
}
</style>